package ai.mspbots.poc.controller.v1;

import ai.mspbots.poc.core.Response;
import ai.mspbots.poc.db.entity.SearchHistoryLog;
import ai.mspbots.poc.db.service.SearchHistoryLogService;
import ai.mspbots.poc.dto.SearchHistoryLogDTO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/v1/searchHistoryLog")
public class SearchHistoryLogController {


    @Autowired
    private SearchHistoryLogService searchHistoryLogService;

    @GetMapping("/list")
    public Response<List<SearchHistoryLog>> listResponse() {
        IPage page = Page.of(1, 10);
        LambdaQueryWrapper<SearchHistoryLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(SearchHistoryLog::getCreateTime);
        List<SearchHistoryLog> list = searchHistoryLogService.list(page, queryWrapper);
        return Response.success(list);
    }


    @PostMapping("/create")
    public Response<Void> create(@RequestBody SearchHistoryLogDTO searchHistoryLogDTO) {
        SearchHistoryLog searchHistoryLog = new SearchHistoryLog();
        searchHistoryLog.setSearchText(searchHistoryLogDTO.getSearchText());
        searchHistoryLog.setCreateTime(new Date());
        searchHistoryLogService.save(searchHistoryLog);
        return Response.ok("保存搜索历史成功");
    }
}
